<?php

declare (strict_types=1);

namespace App\Model;

use PhpParser\Node\Expr\Cast\Object_;

/**
 * @property int $id
 * @property string $title
 * @property string $content
 */
class Policy extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'policy';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['id', 'title', 'content'];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer'];

    public static function getPolicy()
    {
        $lang = getLang();
        $cacheKey = "policy:" . getLang();

        $data = cache()->get($cacheKey);
        if (is_null($data)) {

            $data = null;
            $listInfo = self::where(['lang' => $lang])->select(['title', 'id','type'])->get()->toArray();

            foreach ($listInfo as $k=>$v){
                if ($v['type'] == 1){
                    $v['url'] = 'https://h5.soulcare.plus/term';
                    $data['term'] = $v;
                }
                if ($v['type'] == 2){
                    $v['url'] = 'https://h5.soulcare.plus/policy';
                    $data['policy'] = $v;
                }
            }



            if (!empty($data)) {
                cache()->set($cacheKey, $data);
            }

        }

        return $data;

    }
}
